﻿using System.ComponentModel;
using System.Drawing;
using Newtonsoft.Json;
using ProjectN.Core.Element.Def;
using ProjectN.Core.Instance.Data;

namespace ProjectN.Core.Element
{
    [JsonObject(MemberSerialization = MemberSerialization.OptIn)]
    public class Polygon : BasicElement
    {
        [Category("基本"), DisplayName("画笔"), JsonProperty]
        public required PenInfo Pen { get; set; }

        [Category("基本"), DisplayName("关键点"), JsonProperty]
        public required PointF[] Points { get; set; }

        [Category("基本"), DisplayName("填充"), JsonProperty]
        public required BrushInfo? Fill { get; set; }
        public Polygon()
        {
            this.Draw += Polygon_Draw;
            this.Load += Polygon_Load;
        }
        private void Polygon_Draw(object sender, DrawEventArgs e)
        {
            if (Fill != null)
                e.Graphics.FillPolygon(Utilities.ColoredBrush(Fill.Create(), e.Color), [.. Points]);
            e.Graphics.DrawPolygon(Utilities.ColoredPen(Pen.Create(), e.Color), [.. Points]);
        }
        private void Polygon_Load(object sender, LoadEventArgs e)
        {
            Data = e.InstanceData;
        }

        protected override void UpdateData(InstanceData data)
        {
            Pen.Data = data;
            if(Fill != null)
                Fill.Data = data;
        }

        public override string ToString() => "多边形";
    }
}
